package com.room.search.service;

import com.room.search.config.MyApplicationJavascriptHttpMessageConverter;
import com.room.search.domain.RoomGoods;
import com.room.search.domain.enumeration.BkRoomTypeEnum;
import com.room.search.domain.enumeration.file.FileType;
import com.room.search.service.dto.RoomGoodsDTO;
import com.room.search.service.dto.baidu.BaiduApiResponse;
import com.room.search.service.dto.baidu.BaiduAroundResultDTO;
import com.room.search.service.dto.baidu.BaiduGeoDTO;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * By   : ghd.bright
 * Time : 2022/3/10
 * Description:
 */
class BaiduServiceIT {


    public ClientHttpRequestFactory clientHttpRequestFactory() {
        //用来自Apache HttpClient的默认requestFactory替换默认的requestFactory（它将动态解码GZIP）：
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build());
        factory.setReadTimeout(5000);
        factory.setConnectTimeout(5000);
        return factory;
    }

    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {

        //// 添加拦截器，使用 gzip 编码提交
        ClientHttpRequestInterceptor interceptor = (httpRequest, bytes, execution) -> {
            httpRequest.getHeaders().set("user-agent",   "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
            httpRequest.getHeaders().set(HttpHeaders.ACCEPT,  MediaType.APPLICATION_JSON_VALUE);
            httpRequest.getHeaders().set(HttpHeaders.ACCEPT_ENCODING, "gzip");   // 使用 gzip 编码提交
            return execution.execute(httpRequest, bytes);
        };
        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.getMessageConverters().add(new MyApplicationJavascriptHttpMessageConverter());
//        restTemplate.getMessageConverters().add(new MyApplicationJavascriptHttpMessageConverter());
        restTemplate.getInterceptors().add(interceptor);
        return restTemplate;
    }

    private static final String url = "https://api.map.baidu.com/geoconv/v1/";
    private static final String ak = "c0WAHmoHc7e5ekNrPOt4AbMtz7kyCa97";
    private String coords = "26.0471255,119.33022111";

//    https://api.map.baidu.com/geoconv/v1/?coords=114.21892734521,29.575429778924&from=1&to=5&ak=你的密钥

    @Test
    void test() {

//        ParameterizedTypeReference<BaiduApiResponse> typeReference = new ParameterizedTypeReference<>() {};

        BaiduApiResponse object = restTemplate(clientHttpRequestFactory()).getForObject(
            "https://api.map.baidu.com/place/v2/search?query=教育&location=25.062413,117.030062&radius=1000&output=json&ak=c0WAHmoHc7e5ekNrPOt4AbMtz7kyCa97"
        , BaiduApiResponse.class);
        System.out.println(object);
    }


    @Test
    void rest() {
        System.out.println(FileType.getTypeByExtension("jpg"));

    }
}
